package com.cloudspace.jindun.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.dianhua.LinkmanActivity;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.Person;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class EmergencyPersonAdatper extends BaseAdapter {
    private List<Person> data = new ArrayList<>();

    private FragmentActivity activity;

    public EmergencyPersonAdatper(FragmentActivity activity) {
        this.activity = activity;
    }

    private View.OnClickListener detailListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Person person = (Person) v.getTag();
            if (person != null && activity != null) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("person", person);

                Intent intent = new Intent(activity, LinkmanActivity.class);
                intent.putExtras(bundle);
                activity.startActivity(intent);
            }
        }
    };

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(data)) {
            return 0;
        }

        return data.size();
    }

    public void setData(List<Person> datas) {
        if (ObjUtil.isEmpty(datas)) {
            data.clear();
            notifyDataSetChanged();
            return;
        }

        Collections.sort(datas);

        this.data = datas;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(data)) {
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.emergency_people_item, null);
            holder.position = (TextView) convertView.findViewById(R.id.phone_textview_position);
            holder.name = (TextView) convertView.findViewById(R.id.phone_textview_name);
            holder.detailImg = (ImageView) convertView.findViewById(R.id.phone_detail);
            holder.headIcon = (ImageView) convertView.findViewById(R.id.phone_image_header);
            holder.detailLy = convertView.findViewById(R.id.phone_detail_ly);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.position.setText(data.get(position).getCompany());
        holder.name.setText(data.get(position).getName());
        holder.detailLy.setOnClickListener(detailListener);
        holder.detailLy.setTag(data.get(position));
        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.headIcon, data.get(position).getImageUrl(), Configs.IMAGE_CORNER_RADIUS);
        return convertView;
    }

    static class ViewHolder {
        TextView position;
        TextView name;
        ImageView headIcon;
        ImageView detailImg;
        View detailLy;
    }
}
